﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Reflection;

//自定义
using DreamCube.Foundation.Basic.Utility;

namespace DreamCube.Foundation.WMI
{
    /// <summary>
    /// 访问操作系统的WMI信息
    /// </summary>
    public static class MyWMIManager
    {
        /// <summary>
        /// 获取正在使用的网卡的MAC地址
        /// </summary>
        /// <returns></returns>
        public static String GetNetworkAdapterMACAddress()
        {
            String sql = "select * from Win32_NetworkAdapterConfiguration";
            String macAddr = String.Empty;
            using (ManagementObjectSearcher mo = new ManagementObjectSearcher(sql))
            {
                foreach (var item in mo.Get())
                {
                    using (item)
                    {
                        if (Convert.ToBoolean(item["IPEnabled"]))
                        {
                            if (item["MacAddress"] != null)
                            {
                                macAddr = MyObject.ToStringEx(item["MacAddress"], "");
                                if (!String.IsNullOrEmpty(macAddr))
                                    break;
                            }
                        }
                    }
                }
            }
            return macAddr;
        }

        /// <summary>
        /// 获取磁盘的序列号
        /// </summary>
        /// <returns></returns>
        public static String GetHardDiskSerialNumber()
        {
            String sql = "select * from Win32_PhysicalMedia";
            String diskID = String.Empty;
            using (ManagementObjectSearcher mo = new ManagementObjectSearcher(sql))
            {
                foreach (var item in mo.Get())
                {
                    using (item)
                    {
                        if (item["SerialNumber"] != null)
                        {
                            diskID = MyObject.ToStringEx(item["SerialNumber"], "");
                            if (!String.IsNullOrEmpty(diskID))
                                break;
                        }
                    }
                }
            }
            return diskID;
        }

        /// <summary>
        /// 获取CPU的ID号
        /// </summary>
        /// <returns></returns>
        public static String GetCPUID()
        {
            String cpuid = String.Empty;
            String sql = "select * from Win32_Processor";
            using (ManagementObjectSearcher mo = new ManagementObjectSearcher(sql))
            {
                foreach (var item in mo.Get())
                {
                    using (item)
                    {
                        if (item["ProcessorId"] != null)
                        {
                            cpuid = MyObject.ToStringEx(item["ProcessorId"], "");
                            if (!String.IsNullOrEmpty(cpuid))
                                break;
                        }
                    }
                }
            }
            return cpuid;
        }

    }
}
